package com.rd.sys.action.analysis;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.rd.sys.action.BaseAction;
import com.rd.sys.common.fileexport.ExportUtil;
import com.rd.sys.dto.analysis.statistic.StatisticExportDto;
import com.rd.sys.dto.client.store.Material;
import com.rd.sys.dto.client.store.Result;
import com.rd.sys.dto.commmon.enums.AnalysisEnum;
import com.rd.sys.dto.commmon.enums.ExportEnum;
import com.rd.sys.service.analysis.result.ResultService;
import com.rd.sys.service.analysis.scheme.SchemeService;
import com.rd.sys.service.analysis.statistic.StatisticService;

public class StatisticAction extends BaseAction {
    private static final long serialVersionUID = 230197629544802810L;

    // -------------------------------------以下是数据成员定义-------------------------------------------

    @Autowired
    private StatisticService statisticService;
    @Autowired
    private ResultService resultService;
    @Autowired
    private SchemeService schemeService;

    private String statType;
    private String projectCode;
    private String schemeCode;
    private String resultCode;
    private String materialCode;

    private List<?> statDataList;
    private List<Result> resultList;
    private List<Material> materialList;

    // ------------------------------------------以下是业务方法------------------------------------------

    /**
     * 初次加载统计页面
     *
     * @return
     * @throws Exception
     */
    public String load() throws Exception {
        // 默认展示注视点统计信息
        statType = AnalysisEnum.StatType.FIXATION.getCode();
        statDataList = statisticService.queryFixationDataList(this.getUser(request).getUsercode(), projectCode,
                schemeCode, null, null, this.getPage2Statistic());

        commonQuery();

        return "loadStatistic";
    }

    /**
     * 统计信息主链接
     *
     * @return
     * @throws Exception
     */
    public String index() throws Exception {
        commonQuery();

        if (AnalysisEnum.StatType.FIXATION.getCode().equals(statType)) {
            return fixation();
        } else if (AnalysisEnum.StatType.SACCADE.getCode().equals(statType)) {
            return saccade();
        } else if (AnalysisEnum.StatType.MATERIAL.getCode().equals(statType)) {
            return material();
        } else if (AnalysisEnum.StatType.PUPIL.getCode().equals(statType)) {
            return pupil();
        } else if (AnalysisEnum.StatType.RESULT.getCode().equals(statType)) {
            return result();
        } else if (AnalysisEnum.StatType.EVENT.getCode().equals(statType)) {
            return event();
        }

        throw new Exception("没有对应的统计类型");
    }

    /**
     * 统计信息导出
     *
     * @return
     * @throws Exception
     */
    public void export() throws Exception {
        StatisticExportDto exportDto = new StatisticExportDto(this.getUser(request).getUsercode(), projectCode,
                schemeCode, resultCode, materialCode);

        if (AnalysisEnum.StatType.FIXATION.getCode().equals(statType)) {
            ExportUtil.execute(response, ExportEnum.Type.StatFixationData, exportDto);
        } else if (AnalysisEnum.StatType.SACCADE.getCode().equals(statType)) {
            ExportUtil.execute(response, ExportEnum.Type.StatSaccadeData, exportDto);
        } else if (AnalysisEnum.StatType.MATERIAL.getCode().equals(statType)) {
            ExportUtil.execute(response, ExportEnum.Type.StatMaterialData, exportDto);
        } else if (AnalysisEnum.StatType.PUPIL.getCode().equals(statType)) {
            ExportUtil.execute(response, ExportEnum.Type.StatPupilData, exportDto);
        } else if (AnalysisEnum.StatType.RESULT.getCode().equals(statType)) {
            ExportUtil.execute(response, ExportEnum.Type.StatResultData, exportDto);
        } else if (AnalysisEnum.StatType.EVENT.getCode().equals(statType)) {
            ExportUtil.execute(response, ExportEnum.Type.StatEventData, exportDto);
        } else {
            throw new Exception("没有对应的统计类型,无法执行导出");
        }
    }

    /**
     * 导出结果的原始数据
     *
     * @return
     * @throws Exception
     */
    public void exportRawData() throws Exception {
        StatisticExportDto exportDto = new StatisticExportDto(this.getUser(request).getUsercode(), projectCode,
                schemeCode, resultCode, materialCode);
        ExportUtil.execute(response, ExportEnum.Type.ResultRawData, exportDto);
    }

    /**
     * 注视点统计信息
     *
     * @return
     * @throws Exception
     */
    public String fixation() throws Exception {
        statDataList = statisticService.queryFixationDataList(this.getUser(request).getUsercode(), projectCode,
                schemeCode, resultCode, materialCode, this.getPage2Statistic());
        return "toFixation";
    }

    /**
     * 扫视点统计信息
     *
     * @return
     * @throws Exception
     */
    public String saccade() throws Exception {
        statDataList = statisticService.querySaccadeDataList(this.getUser(request).getUsercode(), projectCode,
                schemeCode, resultCode, materialCode, this.getPage2Statistic());
        return "toSaccade";
    }

    /**
     * 素材详情(某个方案内)
     *
     * @return
     * @throws Exception
     */
    public String material() throws Exception {
        statDataList = statisticService.queryMaterialDataList(this.getUser(request).getUsercode(), projectCode,
                schemeCode, resultCode, materialCode, this.getPage2Statistic());
        return "toMaterial";
    }

    /**
     * 瞳孔统计信息
     *
     * @return
     * @throws Exception
     */
    public String pupil() throws Exception {
        statDataList = statisticService.queryFixationDataList(this.getUser(request).getUsercode(), projectCode,
                schemeCode, resultCode, materialCode, this.getPage2Statistic()); // 瞳孔统计信息就是注视点统计信息的一个子集,故是一个service方法
        return "toPupil";
    }

    /**
     * 结果统计信息
     *
     * @return
     * @throws Exception
     */
    public String result() throws Exception {
        statDataList = statisticService.queryResultDataList(this.getUser(request).getUsercode(), projectCode,
                schemeCode, resultCode, this.getPage2Statistic());
        return "toResult";
    }

    /**
     * 事件统计信息
     *
     * @return
     * @throws Exception
     */
    public String event() throws Exception {
        statDataList = statisticService.queryEventDataList(this.getUser(request).getUsercode(), projectCode,
                schemeCode, resultCode, this.getPage2Statistic());
        return "toEvent";
    }

    /**
     * 获取公共查询条件
     */
    private void commonQuery() throws Exception {
        resultList = resultService.queryResultList(this.getUser(request).getUsercode(), projectCode, schemeCode);
        materialList = schemeService.queryMaterialList(this.getUser(request).getUsercode(), projectCode, schemeCode);
    }

    // ------------------------------------------以下是get/set方法-----------------------------------------
    public String getStatType() {
        return statType;
    }

    public void setStatType(String statType) {
        this.statType = statType;
    }

    public String getMaterialCode() {
        return materialCode;
    }

    public void setMaterialCode(String materialCode) {
        this.materialCode = materialCode;
    }

    public String getResultCode() {
        return resultCode;
    }

    public void setResultCode(String resultCode) {
        this.resultCode = resultCode;
    }

    public List<?> getStatDataList() {
        return statDataList;
    }

    public void setStatDataList(List<?> statDataList) {
        this.statDataList = statDataList;
    }

    public String getProjectCode() {
        return projectCode;
    }

    public void setProjectCode(String projectCode) {
        this.projectCode = projectCode;
    }

    public String getSchemeCode() {
        return schemeCode;
    }

    public void setSchemeCode(String schemeCode) {
        this.schemeCode = schemeCode;
    }

    public List<Material> getMaterialList() {
        return materialList;
    }

    public void setMaterialList(List<Material> materialList) {
        this.materialList = materialList;
    }

    public List<Result> getResultList() {
        return resultList;
    }

    public void setResultList(List<Result> resultList) {
        this.resultList = resultList;
    }

}
